package com.union.vehicleassistant.adapter;

import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.clj.fastble.data.BleDevice;
import com.union.vehicleassistant.R;
import com.union.vehicleassistant.bean.BleConnectState;

import java.util.List;

public class BleAdapter extends BaseQuickAdapter<BleDevice, BaseViewHolder> {

    private int operatePos = -1;
    private BleConnectState operateState = BleConnectState.DEFAULT;

    public BleAdapter(@Nullable List<BleDevice> data) {
        super(R.layout.item_bluetooth_layout, data);

        addChildClickViewIds(R.id.btn_connect);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, BleDevice item, @NonNull List<?> payloads) {
        super.convert(holder, item, payloads);
        ProgressBar progressBar = holder.getView(R.id.connect_progress);
        TextView tvConnect = holder.getView(R.id.btn_connect);
        TextView tvState = holder.getView(R.id.tv_state);
        int itemPosition = getItemPosition(item);
        if (operatePos == itemPosition) {
            if (operateState == BleConnectState.CONNECTING) {
                tvConnect.setVisibility(View.GONE);
                progressBar.setVisibility(View.VISIBLE);
                tvState.setVisibility(View.GONE);
            } else if (operateState == BleConnectState.SUCCESS) {
                progressBar.setVisibility(View.GONE);
                tvConnect.setVisibility(View.GONE);
                tvState.setVisibility(View.VISIBLE);
            }
        } else {
            tvConnect.setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.GONE);
            tvState.setVisibility(View.GONE);
            if (operateState == BleConnectState.CONNECTING) {
                tvConnect.setEnabled(false);
            } else if (operateState == BleConnectState.SUCCESS) {
                tvConnect.setEnabled(true);
            }
        }
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, BleDevice device) {
        baseViewHolder.setText(R.id.tv_ble_name, device.getName());
        baseViewHolder.setText(R.id.tv_mac_addr, device.getMac());
        ProgressBar progressBar = baseViewHolder.getView(R.id.connect_progress);
        TextView tvConnect = baseViewHolder.getView(R.id.btn_connect);
        TextView tvState = baseViewHolder.getView(R.id.tv_state);
        int itemPosition = getItemPosition(device);
        if (operatePos == itemPosition) {
            if (operateState == BleConnectState.CONNECTING) {
                progressBar.setVisibility(View.VISIBLE);
                tvConnect.setVisibility(View.GONE);
                tvState.setVisibility(View.GONE);
            } else if (operateState == BleConnectState.SUCCESS) {
                progressBar.setVisibility(View.GONE);
                tvConnect.setVisibility(View.GONE);
                tvState.setVisibility(View.VISIBLE);
            }
        } else {
            tvConnect.setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.GONE);
            tvState.setVisibility(View.GONE);
            if (operateState == BleConnectState.CONNECTING) {
                tvConnect.setEnabled(false);
            } else if (operateState == BleConnectState.SUCCESS) {
                tvConnect.setEnabled(true);
            }
        }
    }

    public void connectBle(int pos) {
        this.operatePos = pos;
        this.operateState = BleConnectState.CONNECTING;
        notifyItemRangeChanged(0, getData().size());
    }


    public void connectBleSuccess(int pos) {
        this.operatePos = pos;
        this.operateState = BleConnectState.SUCCESS;
        notifyItemRangeChanged(0, getData().size());
    }

    public void resetBleState(int pos) {
        this.operatePos = pos;
        this.operateState = BleConnectState.DEFAULT;
        notifyItemRangeChanged(0, getData().size());
    }
}
